﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using App.Utilities.Web.Handlers;
using System.Data;
using CRMBiz;
using Newtonsoft.Json;

namespace CRMWeb.WebServices
{
    /// <summary>
    /// Summary description for ApplicationInfo
    /// </summary>
    public class ApplicationInfo : BaseHandler
    {

        public override void OnMethodInvoke(OnMethodInvokeArgs e)
        {
            context.Response.Cache.SetCacheability(HttpCacheability.ServerAndNoCache);
            context.Response.Cache.SetExpires(DateTime.MinValue);
            context.Response.Cache.SetNoStore();

            //e.Cancel = true;

            SetResponseContentType(ResponseContentTypes.HTML);
        }

        public override void AfterMethodInvoke(object result)
        {
            base.AfterMethodInvoke(result);
        }


        // Get data From app_info for Application
        public void GetApplicationDetail()
        {
            DataSet ds = new DataSet();
            try
            {
                int input_app_id = string.IsNullOrEmpty(context.Request.QueryString["app_id"]) ? 0 : int.Parse(context.Request.QueryString["app_id"]);
                ds = ApplicationListHandler.GetApplicationDetail(input_app_id);
                string strJson = JsonConvert.SerializeObject(ds.Tables[0]);

                context.Response.Write(strJson);
            }
            catch (Exception)
            {
                throw;
            }
        }
        public void deleteApplication(int app_id)
        {
            bool flag = false;
            try
            {
                flag = ApplicationDetailHandler.DeleteApplication(app_id);
            }
            catch (Exception)
            {
                throw;
            }
        }
        public void deleteMultipleApplications(string applications, int ContactID)
        {
            bool flag = false;
            if (!string.IsNullOrEmpty(applications))
            {
                try
                {
                    flag = ApplicationDetailHandler.DeleteMultipleApplication(applications, ContactID);
                    context.Response.Write(flag);
                }
                catch (Exception)
                {
                    throw;
                }
            }
        }
        public void saveNewApplicationProfile(int ContactID, int NoInApplication, string CombineApp, string DesiredAmount, string OfferedAmount, string app_result, string LoadNo, int NoInApplications, string Folder, string amountMoto, string amoutProperties, string TextAwaitDoc, string purposeSummary,  string memo, string outSrcLenderVal, string referrerVal, string Operator, int IsCarOwner)
        {
            DataSet ds;
            try
            {
                ds = ClientProfileHandler.AddNewApplicationProfile(ContactID, NoInApplication, CombineApp, DesiredAmount, OfferedAmount, app_result, LoadNo, NoInApplications, Folder, amountMoto, amoutProperties, TextAwaitDoc, purposeSummary,  memo, outSrcLenderVal, referrerVal, Operator, IsCarOwner);
                string strJson = JsonConvert.SerializeObject(ds.Tables[0]);
                context.Response.Write(strJson);
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        public void UpdateApplicationProfile(string appid, string desiredAmount, string offeredAmount, string resultDescription, string loadNo, string folder, string motorCount, string propertyCount, string TextAwaitDoc, string purposeSummary,  string memo, string outSrcLenderVal, string referrerVal, string oprator, int IsCarOwner)
        {
            bool response = false;
            try
            {
                response = ClientProfileHandler.UpdateAppDetailData(appid, desiredAmount, offeredAmount, resultDescription, loadNo, folder, motorCount, propertyCount, TextAwaitDoc, purposeSummary, memo, outSrcLenderVal, referrerVal, oprator, IsCarOwner);
                context.Response.Write(response);
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        public void BindApplicationForSMSFields(int app_id)
        {
            try
            {
                DataTable dt = ApplicationDetailHandler.BindApplicationForSMSFields(app_id);
                string strJson = JsonConvert.SerializeObject(dt);

                context.Response.Write(strJson);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public void SelectApplicationProfileData(int app_id, string OperatorID)
        {
            DataTable dt;
            try
            {
                dt = ClientProfileHandler.BindDataTableForApplication(app_id,OperatorID);
                string strJson = JsonConvert.SerializeObject(dt);
                context.Response.Write(strJson);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public void SelectApplicantDetail(int app_id, int contact_id)
        {
            DataTable dt;
            try
            {
                dt = ClientProfileHandler.GetAllContactOfApplicant(app_id, contact_id);
                string strJson = JsonConvert.SerializeObject(dt);
                context.Response.Write(strJson);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public void BindApplicationForm(int app_id)
        {
            DataTable dt;
            try
            {
                var pageIndex = string.IsNullOrEmpty(context.Request["page"]) ? 1 : int.Parse(context.Request["page"]);
                var pageSize = string.IsNullOrEmpty(context.Request["rows"]) ? 10 : int.Parse(context.Request["rows"]);
                var input_app_id = string.IsNullOrEmpty(context.Request["app_id"]) ? 0 : int.Parse(context.Request["app_id"]);

                dt = ClientProfileHandler.BindAppForms(pageIndex, pageSize, input_app_id);
                var total = (dt.Rows.Count > 0);
                string strJson = JsonConvert.SerializeObject(dt);
                strJson = "{\"total\":\"" + total.ToString() + "\",\"rows\":" + strJson + "}";
                context.Response.Write(strJson);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void RemoveDuplicateApp(int ApplicationId, int AppFormId)
        {
            bool flag = false;
            try
            {
                ClientProfileHandler.RemoveDuplicateData(ApplicationId, AppFormId);
                flag = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            context.Response.Write(flag);
        }

        public void ShowAppNames(int app_id)
        {
            DataTable dt;
            try
            {
                dt = ClientProfileHandler.GetApplicantNames(app_id);
                string strJson = JsonConvert.SerializeObject(dt);
                
                context.Response.Write(strJson);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

    }
}